package com.a2v2.online.store.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Timestamp;

import org.springframework.beans.factory.annotation.Autowired;

import com.a2v2.online.store.common.annotation.UserActivityLogging;
import com.a2v2.online.store.database.dao.contract.IUserSessionDAO;
import com.a2v2.online.store.database.entity.UserSession;
import com.a2v2.online.store.enums.UserActivityType;
import com.a2v2.online.store.enums.UserTypes;
import com.a2v2.online.store.service.constant.ServiceResponseStatus;
import com.a2v2.online.store.service.contract.IAuthenticateService;
import com.a2v2.online.store.service.dto.AuthenticateRequest;
import com.a2v2.online.store.service.dto.AuthenticateResponse;

public class AuthenticateServiceImpl implements IAuthenticateService {

	@Autowired
	IUserSessionDAO userSessionDAO;

	/**
	 * @return the userSessionDAO
	 */
	public IUserSessionDAO getUserSessionDAO() {
		return userSessionDAO;
	}

	/**
	 * @param userSessionDAO
	 *            the userSessionDAO to set
	 */
	public void setUserSessionDAO(IUserSessionDAO userSessionDAO) {
		this.userSessionDAO = userSessionDAO;
	}
	
	@UserActivityLogging(activitytype=UserActivityType.PAY,usertype=UserTypes.NormalUser,activity="Login")
	@Override
	public AuthenticateResponse authenticate(AuthenticateRequest authenticateRequest) {
		AuthenticateResponse authenticateResponse = new AuthenticateResponse();
		Timestamp currentTimestamp=new Timestamp(System.currentTimeMillis());
		
		UserSession lastUserSession  = this.getUserSessionDAO().getLastLoggedInSession(Long.valueOf(authenticateRequest.getToken().getCustomerCredentialId()));
		
		UserSession userSession = new UserSession();
		userSession.setIpAddress(getIpAddString());
		userSession.setAbnormalSession(false);
		userSession.setLoggedIn(currentTimestamp);
		userSession.setLogOut(null);
		//userSession.setSessionId("123");
		userSession.setUserCredentialId(new Long(authenticateRequest.getToken().getCustomerCredentialId()));

		UserSession aUserSession = (UserSession) this.getUserSessionDAO().saveOrUpdate(userSession);
		
		
		authenticateResponse.setToken(authenticateRequest.getToken());		
		authenticateResponse.getToken().setLoginSessionId(aUserSession.getId().toString());
		authenticateResponse.getToken().setLoggedInTime(currentTimestamp);
		authenticateResponse.getToken().setLastLoggedInTime(lastUserSession!=null && lastUserSession.getLogOut()!=null?lastUserSession.getLogOut():null);
		return authenticateResponse;
	}

	@Override
	public AuthenticateResponse updateUserSession(
			AuthenticateRequest authenticateRequest) {
		AuthenticateResponse authenticateResponse = new AuthenticateResponse();
		UserSession userSession = (UserSession) this.getUserSessionDAO()
				.getById(
						new Long(authenticateRequest.getToken()
								.getLoginSessionId()));
		userSession.setLogOut(new Timestamp(System.currentTimeMillis()));
		this.userSessionDAO.update(userSession);
		this.userSessionDAO.flush();
		authenticateResponse.setStatus(ServiceResponseStatus.SUCCESS);
		return authenticateResponse;
	}
	
	private String getIpAddString(){
		  InetAddress thisIp =null;
		try {
			thisIp = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
		  return thisIp.getHostAddress().toString(); 
	}

}
